package com.etlmaster.executor.parser;


import java.util.List;
import java.util.Map;

import com.etlmaster.executor.EtlMasterException;
import com.etlmaster.executor.Job;
import com.etlmaster.executor.JobPool;
import com.etlmaster.executor.boot.ExportJobToXml;
import com.etlmaster.executor.utils.DataCollectionUtils;
import com.etlmaster.executor.utils.LogWriter;
import com.etlmaster.jdbc.JdbcTemplate;

public class XmlJobDbLoader extends XmlJobParser{
	/**
	 * 从proc_step中读取配置信息
	 * 
	 * 部分step需要特殊处理
	 * 1.获取扩展配置数据
	 * 2.对于[输出表步骤]增加建表步骤
	 * 
	 * @param name
	 * @return
	 */
	
	private JdbcTemplate getJdbcTemplate(){
		return new JdbcTemplate();
	}

	@Override
	public void init(String jobId,Map<String,Object> params) {
		JdbcTemplate jdbcTemplate = getJdbcTemplate();
		
		try{
			//jobParam = jdbcTemplate.queryForMap("select * from dacp_dp_job where job_id=? ",new Object[]{jobId});
			String teamColumn="team_code";
			if(params.containsKey("teamCode")){
				teamColumn="'"+params.get("teamCode")+"' "+teamColumn;
			}
			String fetchProcSql=DataCollectionUtils.getProperties("fetchProcInfo");
			jobParam = jdbcTemplate.queryForMap(fetchProcSql,new Object[]{teamColumn,jobId});
			
			
			//String teamCode = procObj.get("team_code");
		}catch(Exception e){
			LogWriter.addLog(jobId,0,"WARN","查询程序{}信息失败",jobId);
			LogWriter.addLog(jobId,0,"ERROR","异常：{}",e.getMessage());
			throw new EtlMasterException(e.getMessage());
		}
		String fetchProcStepSql=DataCollectionUtils.getProperties("fetchProcStepInfo");
		rowList = jdbcTemplate.queryForList(fetchProcStepSql,new Object[]{jobId});
		if(rowList==null||rowList.size()==0){
			LogWriter.addLog(jobId,0,"ERROR","未查到程序步骤信息！");
			throw new EtlMasterException("未查到程序步骤信息！");
		}
	}

	@Override
	public Job builder(String jobId, Map<String, Object> params) {
		String jobName = (String) params.get("f");
		Job job = JobPool.getJob(jobName);
		XmlJobDbLoader xmlJobDbLoader = new XmlJobDbLoader();
		job = JobBuilder.build(jobName, params, xmlJobDbLoader, job);
		String export = params.get("export") != null ? params.get("export")
				.toString() : "flase";
		if (export.equalsIgnoreCase("true"))
			ExportJobToXml.export(xmlJobDbLoader);
		return job;
	}
	@Override
	public List<Map<String, Object>> findSenderStepCfg(String stepId){
		JdbcTemplate jdbcTemplate = getJdbcTemplate();
		String sql =  " select id,sender_step_name,comp_id,process_cfg from dps_sender_step where id='"+stepId+"'";
		return jdbcTemplate.queryForList(sql);
	}
}